package xiangMu.car;

import java.util.Scanner;

public class RentalCars {
    private String name;    //汽车租赁中心名字
    private String address;
    private String phone;

    Scanner input = new Scanner(System.in);
    private int countCars = 4;  //轿车数组长度
    private int countBuses = 4; //客车数组长度
    Car[] cars =
            {
                    new Car("京NY28588", "宝马", 800, 5, "X6", true, 0),
                    new Car("京CNY28588", "宝马", 600, 5, "550i", true, 0),
                    new Car("京NT37465", "别克", 300, 5, "林荫大道", true, 0),
                    new Car("京NT96968", "别克", 600, 5, "GL8", true, 0)
            };
    Bus[] buses = {
            new Bus("京6566754", "金杯中客", 800, 16, true, 0),
            new Bus("京9696996", "金杯中客", 1500, 34, true, 0),
            new Bus("京8696997", "金龙大客", 800, 16, true, 0),
            new Bus("京8696998", "金龙大客", 1500, 34, true, 0)
    };

    public RentalCars(String name, String address, String phone) {  //带参构造方法
        this.name = name;
        this.address = address;
        this.phone = phone;
    }

    //查看车辆情况
    public void show(int number) {
        if (number == 1) {
            for (Car car : cars) {
                if (car != null) {
                    System.out.print(car);
                    if (car.isState())
                        System.out.println("当前可租");
                    else
                        System.out.println("已出租");
                }
            }
        } else if (number == 2) {
            for (Bus bus : buses) {
                if (bus != null) {
                    System.out.print(bus);
                    if (bus.isState())
                        System.out.println("当前可租");
                    else
                        System.out.println("已出租");
                }
            }
        }
    }


    public Auto rentAuto() {    //车辆租赁，返回值是车辆对象
        System.out.print("请输入要租赁的类型(轿车/客车):");
        String sort;
        while (!((sort = input.next()).equals("轿车") || sort.equals("客车")))
            System.out.print("类型选择错误，请重新输入:");
        if (sort.equals("轿车"))
            show(1);
        else
            show(2);
        System.out.print("请输入要租赁的车辆，请输入车牌号:");
        String carNumber = input.next();
        if (sort.equals("轿车")) {
            for (int i = 0; i < cars.length && cars[i] != null; i++) {       //防止数组越界异常ArrayIndexOutOfBoundsException和对象空指针异常NullPointerException
                if (cars[i].getCarNumber().equals(carNumber)) {
                    if (!cars[i].isState()) {
                        System.out.println("租赁失败，车辆在出租状态");
                        return null;
                    }
                    System.out.println("您租赁的车型是" + cars[i].getBrand() + "，车牌号：" + cars[i].getCarNumber() + "," + cars[i].getSeatNumber() + "座");
                    System.out.print("请输入租赁的天数:");
                    int days;
                    while ((days = input.nextInt()) <= 0)
                        System.out.print("租赁天数输入错误，请重新输入:");
                    System.out.println("请支付" + cars[i].calcRent(days) + "元");
                    System.out.println("用车愉快");
                    cars[i].setState(false);
                    countCars--;
                    return cars[i];
                }
            }
            System.out.println("租赁失败，无此车型可出租");
        } else {
            for (int i = 0; i < buses.length && buses[i] != null; i++) {        //防止数组越界异常ArrayIndexOutOfBoundsException和对象空指针异常NullPointerException
                if (buses[i].getCarNumber().equals(carNumber)) {
                    System.out.println("您租赁的车型是" + buses[i].getBrand() + "，车牌号为" + buses[i].getCarNumber() + "," + buses[i].getSeatNumber() + "座");
                    System.out.print("请输入租赁的天数:");
                    int days;
                    while ((days = input.nextInt()) <= 0)
                        System.out.print("租赁天数输入错误，请重新输入:");
                    System.out.println("请支付" + buses[i].calcRent(days) + "元");
                    System.out.println("用车愉快");
                    buses[i].setState(false);
                    countBuses--;
                    return buses[i];
                }
            }
            System.out.println("租赁失败，无此车型可出租");
        }
        return null;
    }


    public void returnAuto() {   //车辆归还
        System.out.print("输入归还车辆的类型(轿车/客车):");
        String sort;
        while (!((sort = input.next()).equals("轿车") || sort.equals("客车")))
            System.out.print("类型选择错误，请重新输入:");
        if (sort.equals("轿车")) {
            System.out.print("输入归还车辆的车牌号:");
            String carNumber = input.next();
            for (int i = 0; i < cars.length && cars[i] != null; i++) {       //防止数组越界异常ArrayIndexOutOfBoundsException和对象空指针异常NullPointerException
                if (cars[i].getCarNumber().equals(carNumber)) {
                    if (cars[i].isState()) {    //cars[i].isState()==true,所归还的轿车并未出租，归还的车牌号错误
                        return;
                    }
                    cars[i].setState(true);
                    cars[i].setRecord(cars[i].getRecord() + 1);
                    countCars++;
                    System.out.println("还车成功,下次继续合作");
                    return;
                }
            }
        } else {
            System.out.print("输入归还车辆的车牌号:");
            String carNumber = input.next();
            for (int i = 0; i < buses.length && buses[i] != null; i++) {         //防止数组越界异常ArrayIndexOutOfBoundsException和对象空指针异常NullPointerException
                if (buses[i].getCarNumber().equals(carNumber)) {
                    if (buses[i].isState()) {    //buses[i].isState()==true,所归还的客车并未出租，归还的车牌号错误
                        return;
                    }
                    buses[i].setState(true);
                    buses[i].setRecord(buses[i].getRecord() + 1);
                    countBuses++;
                    System.out.println("还车成功,下次继续合作");
                    return;
                }
            }
        }
        System.out.println("归还失败，请核对信息");
    }

    public void delAuto() {   //车辆报废
        System.out.print("请输入要报废的类型(轿车/客车):");
        String sort;
        while (!((sort = input.next()).equals("轿车") || sort.equals("客车")))
            System.out.print("类型选择错误，请重新输入:");
        if (sort.equals("轿车")) {
            System.out.print("请输入报废轿车的车牌号:");
            String carNumber = input.next();
            for (int i = 0; i < cars.length && cars[i] != null; i++) {       //防止数组越界异常ArrayIndexOutOfBoundsException和对象空指针异常NullPointerException
                if (cars[i].getCarNumber().equals(carNumber)) {
                    if (!cars[i].isState()) {
                        System.out.println("报废失败，车辆在出租状态");
                        return;
                    }
                    int k = 0;
                    for (; k < cars.length && cars[k] != null; k++)
                        ;    //找到数组对象元素为空的下标,语句结束，k最大为k==cars.length，且防止数组越界异常和对象空指针异常
                    for (int j = i; j < k - 1; j++) {
                        cars[j] = cars[j + 1];
                    }
                    cars[k - 1] = null;
                    System.out.println("轿车" + carNumber + "报废成功");
                    countCars--;
                    return;
                }
            }
        } else {
            System.out.print("请输入报废客车的车牌号:");
            String carNumber = input.next();
            for (int i = 0; buses[i] != null; i++) {
                if (buses[i].getCarNumber().equals(carNumber)) {
                    if (!buses[i].isState()) {
                        System.out.println("报废失败，车辆在出租状态");
                        return;
                    }
                    int k = 0;
                    for (; k < buses.length && buses[k] != null; k++)
                        ;    //找到数组对象元素为空的下标,语句结束，k最大为k==buses.length，且防止数组越界异常和对象空指针异常
                    for (int j = i; j < k - 1; j++) {
                        buses[j] = buses[j + 1];
                    }
                    buses[k - 1] = null;
                    System.out.println("客车" + carNumber + "报废成功");
                    countBuses--;
                    return;
                }
            }
        }
        System.out.println("未找到要报废车辆的车牌号，报废失败");
    }


    public void addAuto() {     //采购新车
        expandAuto();   //先检查数组是否满了，如果满了就进行扩容,在加新车前检查数组长度，这样在加新车时，数组永远不会满
        System.out.print("请输入要增加的车辆类型(轿车/客车):");
        String sort;
        while (!((sort = input.next()).equals("轿车") || sort.equals("客车")))
            System.out.print("类型选择错误，请重新输入:");
        if (sort.equals("轿车")) {
            int k = 0;
            for (; k < cars.length && cars[k] != null; k++) ;    //找到数组对象元素为空的下标,语句结束，cars[k]==null,且防止数组越界异常和对象空指针异常
            cars[k] = new Car();
            System.out.print("请输入新轿车的车牌号:");
            cars[k].setCarNumber(input.next());
            System.out.print("请输入新轿车的品牌:");
            cars[k].setBrand(input.next());
            System.out.print("请输入新轿车的日租金:");
            cars[k].setDayRent(input.nextInt());
            System.out.print("请输入新轿车的座位数:");
            cars[k].setSeatNumber(input.nextInt());
            System.out.print("请输入新轿车的型号:");
            cars[k].setType(input.next());
            cars[k].setState(true); //初始出租状态默认为未出租,布尔值为true
            cars[k].setRecord(0);   //初始租赁次数为0
            countCars++;    //数组长度动态变化
        } else {
            int k = 0;
            for (; k < buses.length && buses[k] != null; k++) ;
            //找到数组对象元素为空的下标,语句结束，buses[k]==null,cars[k]==null,且防止数组越界异常和对象空指针异常
            buses[k] = new Bus();
            System.out.print("请输入新客车的车牌号:");
            buses[k].setCarNumber(input.next());
            System.out.print("请输入新客车的品牌:");
            buses[k].setBrand(input.next());
            System.out.print("请输入新客车的日租金:");
            buses[k].setDayRent(input.nextInt());
            System.out.print("请输入新客车的座位数:");
            buses[k].setSeatNumber(input.nextInt());
            buses[k].setState(true); //初始出租状态默认为未出租,布尔值为true
            buses[k].setRecord(0);   //初始租赁次数为0
            countBuses++;     //数组长度动态变化
        }
    }


    public void expandAuto() {   //给数组扩容
        int k = 0;
        for (; k < cars.length && cars[k] != null; k++) ;    //找到数组对象元素为空的下标,语句结束，k最大为k==cars.length，且防止数组越界异常和对象空指针异常
        if (k >= cars.length) {
            Car[] cars2 = new Car[cars.length * 2];
            for (int i = 0; i < k; i++) {
                cars2[i] = cars[i];
            }
            cars = cars2;
        }
        int k2 = 0;
        for (; k2 < buses.length && buses[k2] != null; k2++)
            ;    //找到数组对象元素为空的下标，语句结束，k最大为k==buses.length，且防止数组越界异常和对象空指针异常
        if (k2 >= buses.length) {
            Bus[] buses2 = new Bus[buses.length * 2];
            for (int i = 0; i < k2; i++) {
                buses2[i] = buses[i];
            }
            buses = buses2;
        }
    }

    public void getAutoRecord() {  //查看租赁记录
        boolean a = false;   //是否有车辆租赁记录
        for (Car car : cars) {
            if (car != null) {
                if (car.getRecord() > 0) {
                    System.out.println(car.getBrand() + car.getType() + ",车牌号:" + car.getCarNumber() + ",已完成租赁次数为:" + car.getRecord());
                    a = true;
                }
            }
        }
        for (Bus bus : buses) {
            if (bus != null) {
                if (bus.getRecord() > 0) {
                    System.out.println(bus.getBrand() + ",车牌号:" + bus.getCarNumber() + ",已完成租赁次数为:" + bus.getRecord());
                    a = true;
                }
            }
        }
        if (!a)
            System.out.println("暂无车辆有已完成的租赁记录");
    }

    public int getsumAuto() {   //计算车辆总数
        return countCars + countBuses;
    }

    @Override
    public String toString() {
        return name + "欢迎您，请致电" + phone + "洽谈相关租赁业务";
    }
}

